package com.oa.service.impl;

import com.oa.bean.User;
import com.oa.dao.UserDao;
import com.oa.service.UserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User login(User user) {

        String username = user.getUsername();
        String password = user.getPassword();

        DetachedCriteria dc = DetachedCriteria.forClass(User.class);

        dc.add(Restrictions.eq("username", username));
        dc.add(Restrictions.eq("password", password));

        return userDao.findByCriteria(dc);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public Integer validataUsername(String username) {
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        dc.add(Restrictions.eq("username", username));
        User user = userDao.findByCriteria(dc);
        if (user != null) {
            return 1;
        }
        return 0;
    }
}
